package com.yxcz.serviceImpl;


import java.util.ArrayList;
import java.util.HashMap;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.yxcz.dao.UserDAO;
import com.yxcz.exception.PasswordNotCorrectException;
import com.yxcz.model.User;
import com.yxcz.service.LoginService;


@Service
public class LoginServiceImpl implements LoginService {
	
	@Resource
	private UserDAO userDao;

	@Override
	public boolean checkAccountNameIsExits(String accountName) {
	    User user=userDao.findByAccountName(accountName);
	    if(user==null)
		return false;
	    else
	    return true;
	}

	@Override
	public User getUserInfo(String accountName) {

		return userDao.findByAccountName(accountName);
	}

	@Override
	public void login(User user) throws Exception{
		if(userDao.findByAccountNameAndPassword(user.getAccountName(),user.getPassword())==null)
			throw new PasswordNotCorrectException("密码不正确或未启用!");
	}

	@SuppressWarnings("unchecked")
	@Override
	public ArrayList<User> getUserByQQOpenId(String userOpenID) {
		HashMap<String,Object> vars= new HashMap<>();
		vars.put("1",userOpenID);
		return (ArrayList<User>)userDao.query("from User user where user.qqOpenID=?1",vars);
		
	}

	@Override
	public User authorizationLogin(User user) throws Exception {
		User rtuser =userDao.findByAccountNameAndPassword(user.getAccountName(),user.getPassword());
		if(rtuser==null)
			throw new PasswordNotCorrectException("密码不正确或未启用!");
		return rtuser;
	}

	@Override
	public void bind(User rtuser, String bindType, String openid) {
		if(bindType.equals("QQ"));
		rtuser.setQqOpenID(openid);
		userDao.save(rtuser);
		
	}
	

}
